package com.youkeyi.ddy.cloud.application.business.hr.employee.training.controller;

import com.youkeyi.ddy.cloud.common.api.application.business.hr.employee.training.BusinessEmployeeTrainingCloudService;
import com.youkeyi.ddy.cloud.common.api.application.business.hr.employee.training.dto.BusinessEmployeeTrainingListCloudDto;
import com.youkeyi.ddy.cloud.common.api.application.business.hr.employee.training.dto.BusinessEmployeeTrainingListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.application.business.hr.employee.training.param.BusinessEmployeeTrainingCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.business.hr.employee.training.param.BusinessEmployeeTrainingDeleteCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.business.hr.employee.training.param.BusinessEmployeeTrainingListCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.business.hr.employee.training.param.BusinessEmployeeTrainingUpdateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.training.HrEmployeeTrainingCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.training.dto.HrEmployeeTrainingListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.training.param.HrEmployeeTrainingCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.training.param.HrEmployeeTrainingDeleteCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.training.param.HrEmployeeTrainingQueryCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.training.param.HrEmployeeTrainingUpdateCloudParam;
import com.youkeyi.ddy.cloud.common.constant.YoukeyiDdyCloudBasicPublicConstant;
import com.youkeyi.ddy.cloud.common.context.YoukeyiSystemContext;
import com.youkeyi.ddy.cloud.common.enums.ResponseCodeEnum;
import com.youkeyi.ddy.cloud.common.response.ResponseContent;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * BusinessEmployeeTrainingController
 *
 * @Author liuxiawang
 * @Date 2024/4/30 9:54 AM
 * @Location shenzhen.china
 */
@Service
public class BusinessEmployeeTrainingController implements BusinessEmployeeTrainingCloudService {
    
    @Autowired
    private HrEmployeeTrainingCloudService hrEmployeeTrainingCloudService;

    @Override
    public BusinessEmployeeTrainingListCloudDto list(BusinessEmployeeTrainingListCloudParam param) {
        BusinessEmployeeTrainingListCloudDto dto = new BusinessEmployeeTrainingListCloudDto();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            dto.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return dto;
        }
        if (null == param) {
            dto.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return dto;
        }

        HrEmployeeTrainingQueryCloudParam employeeQueryCloudParam = new HrEmployeeTrainingQueryCloudParam();
        employeeQueryCloudParam.setEmployeeTrainingPrimaryKeys(param.getEmployeeTrainingPrimaryKeys());
        employeeQueryCloudParam.setEmployeePrimaryKeys(param.getEmployeePrimaryKeys());
        employeeQueryCloudParam.setPageSize(param.getPageSize());
        employeeQueryCloudParam.setCurrentPage(param.getCurrentPage());
        HrEmployeeTrainingListCloudDto employeeDtos = hrEmployeeTrainingCloudService.query(employeeQueryCloudParam);
        if (null != employeeDtos && CollectionUtils.isNotEmpty(employeeDtos.getRows())) {
            List<BusinessEmployeeTrainingListItemCloudDto> items = new ArrayList<>();
            employeeDtos.getRows().forEach(employee -> {
                BusinessEmployeeTrainingListItemCloudDto item = new BusinessEmployeeTrainingListItemCloudDto();
                item.setEmployeeTrainingPrimaryKey(employee.getEmployeeTrainingPrimaryKey());
                item.setEmployeeKey(employee.getEmployeeKey());
                item.setStartDate(employee.getStartDate());
                item.setEndDate(employee.getEndDate());
                item.setTrainingName(employee.getTrainingName());
                item.setTrainingOrganization(employee.getTrainingOrganization());
                item.setRemark(employee.getRemark());
                item.setIsDeleted(employee.getIsDeleted());
                item.setIsDeletedName(YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO.getName(item.getIsDeleted()));
                item.setCreateTime(employee.getCreateTime());
                item.setCreateUserKey(employee.getCreateUserKey());
                item.setCreateUserName(employee.getCreateUserName());
                item.setUpdateTime(employee.getUpdateTime());
                item.setUpdateUserKey(employee.getUpdateUserKey());
                item.setUpdateUserName(employee.getUpdateUserName());
                items.add(item);
            });
            dto.setRows(items);
            dto.setTotal(employeeDtos.getTotal());
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }

    @Override
    public ResponseContent create(BusinessEmployeeTrainingCreateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param
                || StringUtils.isBlank(param.getEmployeeKey())
        ) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        HrEmployeeTrainingCreateCloudParam employeeCreateCloudParam = new HrEmployeeTrainingCreateCloudParam();
        employeeCreateCloudParam.setEmployeeKey(param.getEmployeeKey());
        employeeCreateCloudParam.setStartDate(param.getStartDate());
        employeeCreateCloudParam.setEndDate(param.getEndDate());
        employeeCreateCloudParam.setTrainingName(param.getTrainingName());
        employeeCreateCloudParam.setTrainingOrganization(param.getTrainingOrganization());
        employeeCreateCloudParam.setRemark(param.getRemark());
        employeeCreateCloudParam.setCreateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        employeeCreateCloudParam.setCreateUserName(YoukeyiSystemContext.getUserName());
        return hrEmployeeTrainingCloudService.create(employeeCreateCloudParam);
    }

    @Override
    public ResponseContent update(BusinessEmployeeTrainingUpdateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param
                || StringUtils.isBlank(param.getEmployeeTrainingPrimaryKey())
        ) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        HrEmployeeTrainingUpdateCloudParam employeeUpdateCloudParam = new HrEmployeeTrainingUpdateCloudParam();
        employeeUpdateCloudParam.setEmployeeTrainingPrimaryKey(param.getEmployeeTrainingPrimaryKey());
        employeeUpdateCloudParam.setStartDate(param.getStartDate());
        employeeUpdateCloudParam.setEndDate(param.getEndDate());
        employeeUpdateCloudParam.setTrainingName(param.getTrainingName());
        employeeUpdateCloudParam.setTrainingOrganization(param.getTrainingOrganization());
        employeeUpdateCloudParam.setRemark(param.getRemark());
        employeeUpdateCloudParam.setUpdateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        employeeUpdateCloudParam.setUpdateUserName(YoukeyiSystemContext.getUserName());
        return hrEmployeeTrainingCloudService.update(employeeUpdateCloudParam);
    }

    @Override
    public ResponseContent delete(BusinessEmployeeTrainingDeleteCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param || CollectionUtils.isEmpty(param.getEmployeeTrainingPrimaryKeys())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        HrEmployeeTrainingDeleteCloudParam employeeDeleteCloudParam = new HrEmployeeTrainingDeleteCloudParam();
        employeeDeleteCloudParam.setEmployeeTrainingPrimaryKeys(param.getEmployeeTrainingPrimaryKeys());
        employeeDeleteCloudParam.setUpdateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        employeeDeleteCloudParam.setUpdateUserName(YoukeyiSystemContext.getUserName());
        return hrEmployeeTrainingCloudService.delete(employeeDeleteCloudParam);
    }
}
